package com.infosys.newSimpleInvertedIndex.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.infosys.newSimpleInvertedIndex.dto.RelativeDTOI;
import com.infosys.newSimpleInvertedIndex.dto.RelativeDTOO;
import com.infosys.newSimpleInvertedIndex.entities.Relative;
import com.infosys.newSimpleInvertedIndex.enums.JobTitleEnum;
import com.infosys.newSimpleInvertedIndex.enums.PoliticalTitleEnum;
import com.infosys.newSimpleInvertedIndex.enums.RelativeTypeEnum;
import com.infosys.newSimpleInvertedIndex.service.IRelativeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 员工关系表 前端控制器
 * </p>
 *
 * @author Tester
 * @since 2024-06-24
 */
@Controller
@RestController
@RequestMapping("/relative")
public class RelativeController {
    @Autowired
    private IRelativeService relativeService;

    //crud
    //create by user id
    @PostMapping("/addRelative")
    public ResponseEntity<RelativeDTOO> addRelative(String employeeId, JobTitleEnum jt, PoliticalTitleEnum pt, RelativeTypeEnum rt) {
        RelativeDTOI relativeDTOI = new RelativeDTOI();
        relativeDTOI.setEmployeeId(employeeId);
        relativeDTOI.setJobTitle(jt);
        relativeDTOI.setPoliticalTitle(pt);
        relativeDTOI.setType(rt);
        Relative relative = new Relative(relativeDTOI);
        boolean result = relativeService.save(relative);
        if (result) {
            RelativeDTOO relativeDTOO = new RelativeDTOO(relative);
            return ResponseEntity.ok(relativeDTOO);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    //update by user id
    @PutMapping("/updateRelative")
    public ResponseEntity<RelativeDTOO> updateRelative(String employeeId, String relativeId, JobTitleEnum jt, PoliticalTitleEnum pt, RelativeTypeEnum rt) {
        RelativeDTOI relativeDTOI = new RelativeDTOI();
        relativeDTOI.setEmployeeId(employeeId);
        relativeDTOI.setJobTitle(jt);
        relativeDTOI.setPoliticalTitle(pt);
        relativeDTOI.setType(rt);
        // NOTE: UPDATE only
        Relative relative = new Relative(relativeId, relativeDTOI);
        // NOTE: UPDATE only
        boolean result = relativeService.updateById(relative);
        if (result) {
            RelativeDTOO relativeDTOO = new RelativeDTOO(relative);
            return ResponseEntity.ok(relativeDTOO);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // delete by id
    @DeleteMapping("/deleteRelative")
    public ResponseEntity<RelativeDTOO> deleteRelative(String id){
        boolean result = relativeService.removeById(id);
        if (result) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @DeleteMapping("/batchDeleteRelative")
    public ResponseEntity<RelativeDTOO> batchDeleteRelative(List<String> ids){
        boolean result = relativeService.removeByIds(ids);
        if (result) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/getByEmployeeId")
    public ResponseEntity<List<RelativeDTOO>> getRelativeByEmployeeId(String employeeId) {
        List<Relative> relative = relativeService.list(new QueryWrapper<Relative>().eq("employee_id", employeeId));
        List<RelativeDTOO> relativeDTOOs = new ArrayList<>();
        for (Relative r : relative) {
            relativeDTOOs.add(new RelativeDTOO(r));
        }
        return ResponseEntity.ok(relativeDTOOs);
    }

    @GetMapping("/getByJobTitle")
    public ResponseEntity<List<RelativeDTOO>> getRelativeByJobTitle(JobTitleEnum jobTitle) {
        List<Relative> relative = relativeService.list(new QueryWrapper<Relative>().eq("job_title", jobTitle));
        List<RelativeDTOO> relativeDTOOs = new ArrayList<>();
        for (Relative r : relative) {
            relativeDTOOs.add(new RelativeDTOO(r));
        }
        return ResponseEntity.ok(relativeDTOOs);
    }

    @GetMapping("/getByPoliticalTitle")
    public ResponseEntity<List<RelativeDTOO>> getRelativeByPoliticalTitle(PoliticalTitleEnum politicalTitle) {
        List<Relative> relative = relativeService.list(new QueryWrapper<Relative>().eq("political_title", politicalTitle));
        List<RelativeDTOO> relativeDTOOs = new ArrayList<>();
        for (Relative r : relative) {
            relativeDTOOs.add(new RelativeDTOO(r));
        }
        return ResponseEntity.ok(relativeDTOOs);
    }

}
